using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class GlobalVariableSetterInstruction:InstructionBase
    {
        #region props
        private List<FieldSetter> _Setters;
        /// <summary>
        /// Global variable name (TgtFieldName) --> FieldSetter
        /// </summary>
        public List<FieldSetter> Setters
        {
            get { return _Setters; }
            set { _Setters = value; }
        }
	
        #endregion

        /// <summary>
        /// 
        /// </summary>
        public GlobalVariableSetterInstruction():base(Guid.Empty,Guid.Empty)
        {
            this._Setters = null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="componentID"></param>
        /// <param name="jobTicketID"></param>
        /// <param name="setters"></param>
        public GlobalVariableSetterInstruction(Guid componentID, Guid jobTicketID, List<FieldSetter> setters) 
            : base(componentID, jobTicketID)
        {
            this._Setters = setters;
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Instruction");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "ComponentID", this.OwnerComponentID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SubmitTime", this.SubmitTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "StartTime", this.StartTime.ToString());

            if(this.Setters !=null && this.Setters.Count>0)
            {
                XmlNode setterRootNode = XmlDataUtil.AddElement(ref xDoc, xNode, "Setters");
                foreach(FieldSetter fieldSetter in this.Setters)
                {
                    XmlNode setterNode = XmlDataUtil.AddElement(ref xDoc, setterRootNode, "Setter");
                    XmlDataUtil.UpdateAttribute(ref xDoc,setterNode,"GlobalVariableName", fieldSetter.TgtFieldName);
                    XmlDataUtil.UpdateAttribute(ref xDoc, setterNode, "SrcComponentID", fieldSetter.SrcComponentID.ToString());
                    XmlDataUtil.UpdateAttribute(ref xDoc, setterNode, "SrcFieldName", fieldSetter.SrcFieldName);
                    XmlDataUtil.UpdateAttribute(ref xDoc, setterNode, "TransformationCode",
                                                fieldSetter.TransformationCodeSnippet);
                }
            }

            return xNode;
        }

        public override InstructionBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                GlobalVariableSetterInstruction instruction = new GlobalVariableSetterInstruction();
                instruction.OwnerComponentID = new Guid(xNode.Attributes.GetNamedItem("ComponentID").Value);
                instruction.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                instruction.SubmitTime = DateTime.Parse(xNode.Attributes.GetNamedItem("SubmitTime").Value);
                instruction.StartTime = DateTime.Parse(xNode.Attributes.GetNamedItem("StartTime").Value);

                instruction.Setters = null;
                XmlNodeList setterNodes = xNode.SelectNodes("Setters/Setter");
                if (setterNodes != null && setterNodes.Count > 0)
                {
                    instruction.Setters = new List<FieldSetter>();
                    foreach (XmlNode setterNode in setterNodes)
                    {
                        FieldSetter setter = new FieldSetter();
                        setter.TgtFieldName = setterNode.Attributes.GetNamedItem("GlobalVariableName").Value;
                        setter.SrcComponentID = new Guid(setterNode.Attributes.GetNamedItem("SrcComponentID").Value);
                        setter.SrcFieldName = setterNode.Attributes.GetNamedItem("SrcFieldName").Value;
                        setter.TransformationCodeSnippet = setterNode.Attributes.GetNamedItem("TransformationCode").Value;
                        instruction.Setters.Add(setter);
                    }
                }

                return instruction;
            }
            else
            {
                throw new Exception("Unable to instantiate instruction: invalid type specified in xml node");
            }
        }

        public override Dictionary<string, Type> GetBindableMemberNames(Type[] excludeMemberFromTypes)
        {
            Dictionary<string, Type> bindableMemberNames = base.GetBindableMemberNames(excludeMemberFromTypes);
            if (bindableMemberNames == null)
            {
                bindableMemberNames = new Dictionary<string, Type>();
            }
            if (this.Setters != null && this.Setters.Count > 0)
            {
                foreach (FieldSetter fieldSetter in this.Setters)
                {
                    bindableMemberNames.Add(fieldSetter.TgtFieldName, typeof(string));
                }
            }
            return bindableMemberNames;
        }
    }
}
